package com.itzol.javablog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzol.javablog.config.Result;
import com.itzol.javablog.entity.Img;
import com.itzol.javablog.entity.Post;
import com.itzol.javablog.entity.Postvo;
import com.itzol.javablog.entity.Tag;
import com.itzol.javablog.mapper.Postmapper;
import com.itzol.javablog.mapper.Postvomapper;
import com.itzol.javablog.service.ImgService;
import com.itzol.javablog.service.PostService;
import com.itzol.javablog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class PostController {
    @Autowired
    private Postvomapper postvomapper;
    //获取新增文章的id
    private static Integer id;
    @Autowired
    private PostService service;
    private Object Collection;

    //新增文章
    @PostMapping("/addpost")
    public boolean AddPost(@RequestBody Post post){
        boolean r= service.AddPost(post);
        id= post.getId();
        if (r) {
            return true;
        }
        return false;
    }
    @Autowired
    private TagService tagService;

    //新增标签
    @PostMapping("/addtag")
    public boolean AddTag(@RequestBody String[] list) {
        List<Tag> tagList =new ArrayList<>();
        for (int i=0;i<list.length;i++){
            Tag tag=new Tag();
            tag.setName(list[i]);
            tag.setPid(id);
            tagList.add(tag);
        }
        tagService.saveBatch(tagList);
        return true;
    }
    @Autowired
    private ImgService imgService;
    //新增图片 保存图片链接
    @PostMapping("/addimg")
    public boolean AddImg(@RequestBody String[] img){
        List<Img> imgList =new ArrayList<>();
        for (String s : img) {
            Img img1 = new Img();
            img1.setSrc(s);
            img1.setPid(id);
            imgList.add(img1);
        }
        imgService.saveBatch(imgList);
        return true;
    }
    @Autowired
    private PostService postService;
    @GetMapping("/getpost/{page}/{size}")
    public Page<Post> GetPost(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("id");
        if(page.equals(null)||size.equals(null)){
           return null;
        }
        Page<Post> p = new Page<>(page-1,size);
       return postService.page(p,wrapper);
    }
    @GetMapping("/findAlllist/{page}/{size}")
    //page 页数 size 每页数量
    public List<Postvo> findAlllist(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        return postvomapper.postvo((page-1)*size,size);
    }

    @GetMapping("/getallpost")
    public List<Post> GetPostAll(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("id");
         return postService.list(wrapper);
    }
    @GetMapping("/getpost/{id}")
    public Result GetPost(@PathVariable("id") Integer id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        return Result.succ(postService.getOne(wrapper));
    }
}

